package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.dto.Childs;
import com.itheima.dto.MenusDto;
import com.itheima.dto.PermissionDto;
import com.itheima.dto.Points;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.PermissionMenuExtendMapper;
import com.itheima.mapper.PermissionPermissionGroupMapper;
import com.itheima.mapper.PermissionPointExtendMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.PermissionMenuExtend;
import com.itheima.pojo.PermissionPointExtend;
import com.itheima.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenusServiceImpl implements MenusService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private PermissionMenuExtendMapper permissionMenuExtendMapper;

    @Autowired
    private PermissionPointExtendMapper permissionPointExtendMapper;

    @Autowired
    private PermissionPermissionGroupMapper permissionPermissionGroupMapper;

    @Override
    public List<PermissionDto> menusPage() {
        //查询一级菜单
        LambdaQueryWrapper<Permission> wapper = new LambdaQueryWrapper<>();
        wapper.isNull(Permission::getPid);
        List<Permission> permissions = permissionMapper.selectList(wapper);
        List<PermissionDto> permissionDtos = new ArrayList<>();
        //遍历查询二级菜单
        for (Permission permission : permissions) {
            PermissionDto permissionDto = new PermissionDto();
            permissionDto.setId(permission.getId());
            permissionDto.setPid(permission.getPid());
            permissionDto.setTitle(permission.getName());
            //查询code
            LambdaQueryWrapper<PermissionMenuExtend> wapper1 = new LambdaQueryWrapper<>();
            wapper1.eq(PermissionMenuExtend::getPermissionId,permission.getId());
            PermissionMenuExtend permissionMenuExtend = permissionMenuExtendMapper.selectOne(wapper1);
            permissionDto.setCode(permissionMenuExtend.getCode());
            //查询二级菜单
            LambdaQueryWrapper<Permission> wapper2 = new LambdaQueryWrapper<>();
            wapper2.eq(Permission::getPid,permission.getId());
            List<Permission> permissions1 = permissionMapper.selectList(wapper2);
            List<Childs> childsList = new ArrayList<>();
            //遍历查询功能点
            for (Permission permission1 : permissions1) {
                Childs childs = new Childs();
                childs.setId(permission1.getId());
                childs.setPid(permission1.getPid());
                childs.setTitle(permission1.getName());
                //查询code
                LambdaQueryWrapper<PermissionMenuExtend> wapper3 = new LambdaQueryWrapper<>();
                wapper3.eq(PermissionMenuExtend::getPermissionId,permission1.getId());
                PermissionMenuExtend permissionMenuExtend1 = permissionMenuExtendMapper.selectOne(wapper3);
                childs.setCode(permissionMenuExtend1.getCode());
                //查询功能点
                LambdaQueryWrapper<Permission> wapperPoint = new LambdaQueryWrapper<>();
                wapperPoint.eq(Permission::getPid,permission1.getId());
                List<Permission> permissions2 = permissionMapper.selectList(wapperPoint);
                List<Points> pointsList = new ArrayList<>();
                for (Permission permission2 : permissions2) {
                    Points points = new Points();
                    points.setId(permission2.getId());
                    points.setPid(permission2.getPid());
                    points.setTitle(permission2.getName());
                    //查询code
                    LambdaQueryWrapper<PermissionPointExtend> wapper4 = new LambdaQueryWrapper<>();
                    wapper4.eq(PermissionPointExtend::getPermissionId,permission2.getId());
                    PermissionPointExtend permissionPointExtend = permissionPointExtendMapper.selectOne(wapper4);
                    points.setCode(permissionPointExtend.getCode());
                    pointsList.add(points);
                }
                childs.setPoints(pointsList);
                childsList.add(childs);
            }
         permissionDto.setChilds(childsList);
         permissionDtos.add(permissionDto);
        }
        return permissionDtos;
    }

    @Transactional
    @Override
    public void modifyMenus(MenusDto menusDto) {
        if (menusDto.isIs_point()){
            PermissionPointExtend permissionPointExtend = new PermissionPointExtend();
            permissionPointExtend.setCode(menusDto.getCode());
            permissionPointExtend.setPermissionId(menusDto.getPid());
            LambdaUpdateWrapper<PermissionPointExtend> wapper = new LambdaUpdateWrapper<>();
            wapper.eq(PermissionPointExtend::getPermissionId,permissionPointExtend.getPermissionId());
            permissionPointExtendMapper.update(permissionPointExtend,wapper);
        }else {
            PermissionMenuExtend permissionMenuExtend = new PermissionMenuExtend();
            permissionMenuExtend.setCode(menusDto.getCode());
            permissionMenuExtend.setPermissionId(menusDto.getPid());
            LambdaUpdateWrapper<PermissionMenuExtend> wapper = new LambdaUpdateWrapper<>();
            wapper.eq(PermissionMenuExtend::getPermissionId,permissionMenuExtend.getPermissionId());
            permissionMenuExtendMapper.update(permissionMenuExtend,wapper);
        }

        Permission permission = new Permission();
        permission.setId(menusDto.getId());
        permission.setName(menusDto.getTitle());
        permission.setPid(menusDto.getPid());
        permissionMapper.updateById(permission);

    }

    @Transactional
    @Override
    public void removeMenus(Integer id) {

        permissionMapper.closeForeignKeyChecks();

        Permission permission = permissionMapper.selectById(id);
        //判断是菜单还是功能点
        if (permission.getType() == 2){
            //功能点
            LambdaUpdateWrapper<PermissionPointExtend> wapper = new LambdaUpdateWrapper<>();
            wapper.eq(PermissionPointExtend::getPermissionId,id);
            permissionPointExtendMapper.delete(wapper);



        }else {
            //判断是一级菜单还是二级菜单
            if(permission.getPid() != null){
                //二级菜单,先删除下面的功能点,再删除自己
                LambdaQueryWrapper<Permission> wapper1 = new LambdaQueryWrapper<>();
                wapper1.eq(Permission::getPid,id);
                List<Permission> permissions = permissionMapper.selectList(wapper1);
                for (Permission permission1 : permissions) {

                    LambdaUpdateWrapper<PermissionPointExtend> wapper2 = new LambdaUpdateWrapper<>();
                    wapper2.eq(PermissionPointExtend::getPermissionId,permission1.getId());
                    permissionPointExtendMapper.delete(wapper2);

                    permissionPermissionGroupMapper.deleteById(permission1.getId());
                    permissionMapper.deleteById(permission1.getId());

                }


            }else {
                //一级菜单,先删除二级菜单下面的功能点,再删除二级菜单,最后删除自己
                //查询二级菜单
                LambdaQueryWrapper<Permission> wapper1 = new LambdaQueryWrapper<>();
                wapper1.eq(Permission::getPid,id);
                List<Permission> permissions = permissionMapper.selectList(wapper1);
                for (Permission permission1 : permissions) {
                    //查询功能点
                    LambdaQueryWrapper<Permission> wapper4 = new LambdaQueryWrapper<>();
                    wapper4.eq(Permission::getPid,id);
                    List<Permission> permissions1 = permissionMapper.selectList(wapper4);
                    for (Permission permission2 : permissions1) {

                        LambdaUpdateWrapper<PermissionPointExtend> wapperPoint = new LambdaUpdateWrapper<>();

                        wapperPoint.eq(PermissionPointExtend::getPermissionId,permission2.getId());

                        permissionPointExtendMapper.delete(wapperPoint);

                        permissionPermissionGroupMapper.deleteById(permission2.getId());
                        permissionMapper.deleteById(permission2.getId());

                    }
                    LambdaUpdateWrapper<PermissionMenuExtend> wapperMenus = new LambdaUpdateWrapper<>();
                    wapperMenus.eq(PermissionMenuExtend::getPermissionId,permission1.getId());
                    permissionMenuExtendMapper.delete(wapperMenus);
                    permissionPermissionGroupMapper.deleteById(permission1.getId());
                    permissionMapper.deleteById(permission1.getId());

                }
            }
        }
        LambdaUpdateWrapper<PermissionMenuExtend> wapper = new LambdaUpdateWrapper<>();
        wapper.eq(PermissionMenuExtend::getPermissionId,id);
        permissionMenuExtendMapper.delete(wapper);
        permissionPermissionGroupMapper.deleteById(id);
        permissionMapper.deleteById(id);

        permissionMapper.openForeignKeyChecks();
    }

    @Override
    public void addMenus(MenusDto menusDto) {
        if (menusDto.isIs_point()){
            //存储Permission
            Permission permission = new Permission();
            permission.setPid(menusDto.getPid());
            permission.setName(menusDto.getTitle());
            permission.setCreateTime(LocalDateTime.now());
            permission.setType(2);
            permissionMapper.insert(permission);
            //存储PermissionPointExtend
            PermissionPointExtend permissionPointExtend = new PermissionPointExtend();
            permissionPointExtend.setPermissionId(permission.getId());
            permissionPointExtend.setCode(menusDto.getCode());
            permissionPointExtendMapper.insert(permissionPointExtend);
            //添加关联id
            permission.setPermissionPointExtendId(permissionPointExtend.getId());
            permissionMapper.updateById(permission);
        }else {
            //存储Permission
            Permission permission = new Permission();
            permission.setPid(menusDto.getPid());
            permission.setName(menusDto.getTitle());
            permission.setCreateTime(LocalDateTime.now());
            permission.setType(1);
            permissionMapper.insert(permission);
            //存储PermissionMenuExtend
            PermissionMenuExtend permissionMenuExtend = new PermissionMenuExtend();
            permissionMenuExtend.setPermissionId(permission.getId());
            permissionMenuExtend.setCode(menusDto.getCode());
            permissionMenuExtendMapper.insert(permissionMenuExtend);
            //添加关联id
            permission.setPermissionMenuExtendId(permissionMenuExtend.getId());
            permissionMapper.updateById(permission);
        }
    }

    @Override
    public MenusDto findMenusById(Integer id) {
        Permission permission = permissionMapper.selectById(id);
        MenusDto menusDto = new MenusDto();
        if (permission.getType()==1){
            LambdaQueryWrapper<PermissionMenuExtend> wapper = new LambdaQueryWrapper<>();
            wapper.eq(PermissionMenuExtend::getPermissionId,id);
            PermissionMenuExtend permissionMenuExtend = permissionMenuExtendMapper.selectOne(wapper);
            menusDto.setIs_point(false);
            menusDto.setCode(permissionMenuExtend.getCode());
        }else {
            LambdaQueryWrapper<PermissionPointExtend> wapper = new LambdaQueryWrapper<>();
            wapper.eq(PermissionPointExtend::getPermissionId,id);
            PermissionPointExtend permissionPointExtend = permissionPointExtendMapper.selectOne(wapper);
            menusDto.setIs_point(true);
            menusDto.setCode(permissionPointExtend.getCode());
        }
        menusDto.setId(permission.getId());
        menusDto.setPid(permission.getPid());
        menusDto.setTitle(permission.getName());
        return menusDto;
    }
}
